package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.query.StrategyThemeQueryObject;
import cn.wolfcode.wolf2w.repository.StrategyThemeMongoRepository;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StrategyThemeServiceImpl implements IStrategyThemeService {

    @Autowired
    private StrategyThemeMongoRepository repository;
    @Autowired
    private MongoTemplate template;

    @Override
    public void save(StrategyTheme strategyTheme) {
        strategyTheme.setId(null);
        repository.save(strategyTheme);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public void update(StrategyTheme strategyTheme) {
        repository.save(strategyTheme);
    }

    @Override
    public StrategyTheme find(String id) {
        StrategyTheme strategyTheme = repository.findById(id).orElse(null);

        return strategyTheme;
    }

    @Override
    public List<StrategyTheme> findAll() {
        List<StrategyTheme> list = repository.findAll();
        return list;
    }

    @Override
    public Page query(StrategyThemeQueryObject qo) {
        Query query = new Query();
        long count = template.count(query, StrategyTheme.class);
        if(count == 0){
            return Page.empty();
        }
        Pageable page = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "sequence");
        query.with(page);
        List<StrategyTheme> list = template.find(query, StrategyTheme.class);
        return new PageImpl(list,page,count);
    }

}
